/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
/* eslint no-console: ["error", { allow: ["log"] }] */
const gulp = require('gulp');
const modifyFile = require('gulp-modify-file');
const path = require('path');
const rollup = require('rollup');
const buble = require('rollup-plugin-buble');
const replace = require('rollup-plugin-replace');
const commonjs = require('rollup-plugin-commonjs');
const resolve = require('rollup-plugin-node-resolve');
const vue = require('rollup-plugin-vue');

function buildKs(cb) {
  const env = process.env.NODE_ENV || 'development';
  const target = process.env.TARGET || 'universal';
  const buildPath = env === 'development' ? './build' : './packages';

  let jsVuePath = path.resolve(__dirname, `../${buildPath}/vue/js-vue.esm.js`);
  let jsPath = path.resolve(__dirname, `../${buildPath}/core/js.esm.bundle`);
  if (process.platform.indexOf('win') === 0) {
      jsVuePath = jsVuePath.replace(/\\/g, '/');
      jsPath = jsPath.replace(/\\/g, '/');
  }

  gulp.src('./kitchen-sink/vue/index.html')
      .pipe(modifyFile((content) => {
            if (env === 'development') {
    return content
        .replace('../../packages/core/css/js.min.css', '../../build/core/css/js.css')
        .replace('../../packages/core/js/js.min.js', '../../build/core/js/js.js');
  }
  return content
      .replace('../../build/core/css/js.css', '../../packages/core/css/js.min.css')
      .replace('../../build/core/js/js.js', '../../packages/core/js/js.min.js');
}))
.pipe(gulp.dest('./kitchen-sink/vue'));

  rollup.rollup({
    input: './kitchen-sink/vue/src/app.js',
    plugins: [
      replace({
            delimiters: ['', ''],
            'process.env.NODE_ENV': JSON.stringify(env),
            'process.env.TARGET': JSON.stringify(target),
            "'js-vue'": () => `'${jsVuePath}'`,
            "'js/js.esm.bundle'": () => `'${jsPath}'`,
        }),
      resolve({ jsnext: true }),
      commonjs(),
      vue(),
      buble({
        objectAssign: 'Object.assign',
      }),
    ],
      onwarn(warning, warn) {
        const ignore = ['EVAL'];
        if (warning.code && ignore.indexOf(warning.code) >= 0) {
          return;
        }
        warn(warning);
      },
    }).then(bundle => bundle.write({
        format: 'umd',
        name: 'app',
        strict: true,
        sourcemap: false,
        file: './kitchen-sink/vue/js/app.js',
      })).then(() => {
        if (cb) cb();
    }).catch((err) => {
        console.log(err);
      if (cb) cb();
    });
}

module.exports = buildKs;
